/**
 * Created by Administrator on 2016/3/4.
 */
/**
 * @ Date 2016/02/19 01:09:14
 */
//主场景中bottom栏公告点击触发的弹窗
gpNamespace.GonggaoLayer = cc.Layer.extend({
	tv1: null,
	tv1Touchable: null,
	tv2: null,
	tv2Touchable: null,

	ctor: function () {
		this._super();

		this.init();
	},

	init: function () {
		cc.spriteFrameCache.addSpriteFrames(gpNamespace.pdkgonggaoRes.Pdkgonggao_plist);

		//背景
		var background = new cc.Sprite(gpNamespace.gg.Gg_Bg);
		background.attr({
			x: 96,
			y: 68,
			anchorX: 0,
			anchorY: 0
		});
		this.addChild(background);

		//左边背景
		var leftBg = new cc.Sprite(gpNamespace.gg.Gg_LeftBg);
		leftBg.attr({
			x: 126,
			y: 130,
			anchorX: 0,
			anchorY: 0
		});
		this.addChild(leftBg);

		//右边背景
		/*var rightListBg = new cc.Sprite(gpNamespace.gg.Gg_RightListBg);
        rightListBg.attr({
            x: 446,
            y: 375,
            anchorX: 0,
            anchorY: 0
        });
        this.addChild(rightListBg);*/

		//砸蛋
		/*var eggPic = new cc.Sprite(gpNamespace.gg.Gg_EggPic);
        eggPic.attr({
            x: 36,
            y: 26,
            anchorX: 0,
            anchorY: 0
        });
        rightListBg.addChild(eggPic);*/

		//滚动条
		/*var scrollBar = new cc.Sprite(gpNamespace.gg.Gg_ScrollBar);
		scrollBar.attr({
			x: 1126,
			y: 270,
			anchorX: 0,
			anchorY: 0
		});
		this.addChild(scrollBar);*/

		//公告列表
		var gonggaoList = new tableViewPro.TableView(this, cc.size(720, 380), 0);
		gonggaoList.setPosition(430, 148);
		gonggaoList.setAnchorPoint(0, 0);
		this.addChild(gonggaoList);
		this.gonggaoList = gonggaoList;
		gonggaoList.setTouchEnabled(false);
		gonggaoList.setBarOffset(0, 0);


		gpNamespace.TyNetManager.getInstance().setCddGonggaoLayer(this);
		xinyou.netManager.getGonggao(1);
	},
	setGonggaoContainer:function(gonggao){
		//去除所有空格
		var info=gonggao.itemInfo.replace(/\s+/g," ");
		cc.log("修改后info:"+info);

		var lastIndex = info.lastIndexOf(' ');
		var mainMsg = info.substring(0,lastIndex);//信息
		var lastStr = info.substring(lastIndex + 1);//交流群
//		cc.log(lastStr+":::"+lastStr);    	

		//显示主要信息
		var lineNum=mainMsg.length/8+1;
		for(var i=0;i<lineNum;i++){
			var line = new cc.LabelTTF(mainMsg.substr(i*8,8), gpNamespace.DEFAULTFONT, 25);
			line.setFontFillColor(cc.color(108, 28, 11, 255));
			line.attr({
				x: 160,
				y: 390-50*i,
				anchorX: 0,
				anchorY: 0.5
			});
			this.addChild(line);
		}
		//显示最后交流群信息
		var lastline = new cc.LabelTTF(lastStr, gpNamespace.DEFAULTFONT, 25);
		lastline.setFontFillColor(cc.color(108, 28, 11, 255));
		lastline.attr({
			x: 160,
			y: 400-50*lineNum,
			anchorX: 0,
			anchorY: 0.5
		});
		this.addChild(lastline);

		//信息列表
		cc.log("array长度："+gonggao.itemCount);
		this.gonggaoList.clearData();
		this.gonggaoList.addData({
			"barInfo": {"width": 2, "height": 80},
			"sameItem": [
			             {
			            	 "dataType": 1,
			            	 "headInfo": {
			            		 "headType": 0,
			            		 "offset": 0,
			            		 "imageName": "",
			            		 "width": 0
			            	 },
			            	 "pageInfo": {"colCount": 1},
			            	 "cellInfo": {
			            		 "cellType": 0,
			            		 "cellCount": gonggao.itemCount,
			            		 "imageName": gpNamespace.gg.Gg_RightListBg,
			            		 "height": 146
			            	 }
			             }
			            ]
		});

		this.gonggaoList.showData();
		this.gonggaoList.setTouchEnabled(true);
//		gonggaoListTouchable = true;



		var cellCount = this.gonggaoList.currentItem.cellInfo.cellCount;
		var cellType = this.gonggaoList.currentItem.cellInfo.cellType;
		var currentPage = this.gonggaoList.currentPage;
		cc.log("************"+cellCount+","+cellType+","+currentPage);
		for (var a = 0; a < gonggao.itemCount; a++) {
			var cell;
			switch (cellType) {
			case 0://sprite
				cell = currentPage.getChildByTag(a);
				break;
			case 1://menu
//				cell = currentPage.getChildByName(xzgbNamespace.TVMENU).getChildByTag(a);
				break;
			}
			this.setGonggaoCell(a, cell, gonggao);
		}

	},
	setGonggaoCell:function(index, cell, gonggao){
		var exchangeItems = [];
		var listBgitem = new cc.MenuItemSprite(
				new cc.Sprite(gpNamespace.gg.Gg_RightListBg),
				new tyNamespace.maskSprite(gpNamespace.gg.Gg_RightListBg),
				this.gongGaoDetailCallbackFunction, this);
		listBgitem.attr({
			x: 0,
			y: 0,
			anchorX: 0,
			anchorY: 0
		});
		listBgitem.setUserData(gonggao.notices[index][1]);
		exchangeItems.push(listBgitem);

		var iconPath=gpNamespace.gg.Gg_QqlbPic;
		if(gonggao.notices[index][0]=="7000"){
			iconPath = gpNamespace.gg.Gg_EggPic;
		}else if(gonggao.notices[index][0]=="7001"){
			iconPath = gpNamespace.gg.Gg_PcPic;
		}else if(gonggao.notices[index][0]=="7002"){
			iconPath = gpNamespace.gg.Gg_XsPic;
		}else if(gonggao.notices[index][0]=="7003"){
			iconPath = gpNamespace.gg.Gg_QqlbPic;
		}
		var icon = new cc.Sprite(iconPath);
		icon.attr({
			x: 30,
			y: 80,
			anchorX: 0,
			anchorY: 0.5
		});
		cell.addChild(icon,3);

		var title = new cc.LabelTTF(gonggao.notices[index][3], gpNamespace.DEFAULTFONT, 25);
		title.setFontFillColor(cc.color(108, 78, 11, 255));
		title.attr({
			x: 160,
			y: 105,
			anchorX: 0,
			anchorY: 0.5
		});
		cell.addChild(title,3);

		var content = new cc.LabelTTF(gonggao.notices[index][2], gpNamespace.DEFAULTFONT, 20);
		content.setFontFillColor(cc.color(255,255,255, 255));
		content.attr({
			x: 160,
			y: 60,
			anchorX: 0,
			anchorY: 0.5
		});
		cell.addChild(content,3);
		
		var gongGaoMenu = new tableViewPro.Menu(exchangeItems);
		gongGaoMenu.attr({
			x: 0,
			y: 0,
			anchorX: 0,
			anchorY: 0
		});
		cell.addChild(gongGaoMenu);
//		gongGaoMenu.setMyParent(this.gonggaoList);
		gongGaoMenu.setTouchListener();
		this.scheduleOnce(function () {
			gongGaoMenu.setTouchArea(this.gonggaoList.clippingNode.getContentSize(), this.gonggaoList.clippingNode.getParent().convertToWorldSpace(this.gonggaoList.clippingNode.getPosition()));
		}, 0.5);
	},
	gongGaoDetailCallbackFunction:function(sender){
		noticeId = sender.getUserData();
		switch(noticeId){
		case 2:
			tyNamespace.setMenuEnable(sender);
			var runningScene = cc.director.getRunningScene();
			var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
			var modelLayer = new tyNamespace.ModelLayer(70);
			modelLayer.setTouchArea(cc.size(1140, 480), cc.p(62, 87));
			modelLayerCancelable.addChild(modelLayer);
			runningScene.addChild(modelLayerCancelable, 10);
			var qqlbLayer = new gpNamespace.QqlbLayer();//qq礼包
			qqlbLayer.attr({
				//左上角坐标
				x: 0,
				y: cc.winSize.height
			});
			modelLayer.addChild(qqlbLayer);
			tyNamespace.AnimationManager.nodeJumpShow(qqlbLayer, -cc.winSize.height);
			break;
		case 3:
			tyNamespace.setMenuEnable(sender);
			var runningScene = cc.director.getRunningScene();
			var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
			var modelLayer = new tyNamespace.ModelLayer(70);
			modelLayer.setTouchArea(cc.size(1140, 560), cc.p(66, 94));
			modelLayerCancelable.addChild(modelLayer);
			runningScene.addChild(modelLayerCancelable, 10);
			var zjdLayer = new gpNamespace.ZjdLayer();//砸金蛋
			zjdLayer.attr({
				//左上角坐标
				x: 0,
				y: cc.winSize.height
			});
			modelLayer.addChild(zjdLayer);
			tyNamespace.AnimationManager.nodeJumpShow(zjdLayer, -cc.winSize.height);
			break;
		case 4:
			tyNamespace.setMenuEnable(sender);
			var runningScene = cc.director.getRunningScene();
			var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
			var modelLayer = new tyNamespace.ModelLayer(70);
			modelLayer.setTouchArea(cc.size(555, 490), cc.p(365, 111));
			modelLayerCancelable.addChild(modelLayer);
			runningScene.addChild(modelLayerCancelable, 10);
			var czLayer = new gpNamespace.CzdlbLayer();//超值大礼包
			czLayer.attr({
				//左上角坐标
				x: 0,
				y: cc.winSize.height
			});
			modelLayer.addChild(czLayer);
			tyNamespace.AnimationManager.nodeJumpShow(czLayer, -cc.winSize.height);
			break;
		case 5:
			tyNamespace.setMenuEnable(sender);
			var runningScene = cc.director.getRunningScene();
			var modelLayerCancelable = new tyNamespace.ModelLayerCancelable();
			var modelLayer = new tyNamespace.ModelLayer(70);
			modelLayer.setTouchArea(cc.size(875, 605), cc.p(200, 75));
			modelLayerCancelable.addChild(modelLayer);
			runningScene.addChild(modelLayerCancelable, 10);
			var hhdlbLayer = new gpNamespace.HhdlbLayer();//豪华大礼包
			hhdlbLayer.attr({
				//左上角坐标
				x: 0,
				y: cc.winSize.height
			});
			modelLayer.addChild(hhdlbLayer);
			tyNamespace.AnimationManager.nodeJumpShow(hhdlbLayer, -cc.winSize.height);
			break;
		default:
			console.log("未知NoticeId:"+noticeId);
			break;
		}
	}
});